package com.wan37.logic.chat.handler;

import com.wan37.logic.chat.cache.ChatCache;
import com.wan37.logic.chat.dao.PrivateChatDao;
import com.wan37.logic.chat.service.PrivateChatService;
import com.wan37.logic.chat.util.ChatRouteUtil;
import com.wan37.logic.chat.domain.vo.ChatVO;
import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameChat;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 获取历史聊天信息
 *
 * @author : luoyong
 * @date : 2020-07-15 17:43
 **/
public class CGameOneToOneChatOldHandler extends MsgHandler {
    private ChatCache chatCache;
    private ChatRouteUtil chatRouteUtil;
    private PrivateChatService privateChatService;

    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameOneToOneChatOldHandler(MessageVO messageVO) {
        super(messageVO);
        this.chatCache = SpringContextHolder.getBean("chatCache");
        this.chatRouteUtil = SpringContextHolder.getBean("chatRouteUtil");
        this.privateChatService = SpringContextHolder.getBean("privateChatService");
    }

    @Override
    public void execute(Worker worker) throws Exception {
        /**
         * 将双方记录私聊信息的集合中历史记录拿出，并根据时间排序，发送给请求客户端。
         */
        GameChat.OneToOneChatOld oneToOneChatOld = (GameChat.OneToOneChatOld) messageVO.getMessage();
        List<ChatVO> chatVOS = new ArrayList<>();
        /**
         * 获取对方发送给自身的私聊记录
         */
        if (!chatCache.getUserIdToChatIn().get(messageVO.getUserId())
                .getOneChatVOListMap().containsKey(oneToOneChatOld.getUserId2())) {
            chatCache.getUserIdToChatIn().get(messageVO.getUserId())
                    .getOneChatVOListMap().put(oneToOneChatOld.getUserId2(), new CopyOnWriteArrayList<>());
            /**
             * 从数据库取历史记录
             */
            List<ChatVO> chatVOList = privateChatService.getPrivateChatDOBySenderIdAndReceiverId
                    (messageVO.getUserId(), oneToOneChatOld.getUserId2());
            if (chatVOList != null){
                chatCache.getUserIdToChatIn().get(messageVO.getUserId()).getOneChatVOListMap()
                        .get(oneToOneChatOld.getUserId2()).addAll(chatVOList);
            }
        }
        /**
         * 获取自身发送给对方的消息记录
         */
        if (!chatCache.getUserIdToChatIn().get(oneToOneChatOld.getUserId2())
                .getOneChatVOListMap().containsKey(messageVO.getUserId())) {
            chatCache.getUserIdToChatIn().get(oneToOneChatOld.getUserId2())
                    .getOneChatVOListMap().put(messageVO.getUserId(), new CopyOnWriteArrayList<>());
            List<ChatVO> chatVOList = privateChatService.getPrivateChatDOBySenderIdAndReceiverId
                    (oneToOneChatOld.getUserId2(), messageVO.getUserId());
            if (chatVOList != null){
                chatCache.getUserIdToChatIn().get(oneToOneChatOld.getUserId2()).getOneChatVOListMap()
                        .get(messageVO.getUserId()).addAll(chatVOList);
            }
        }
        chatVOS.addAll(chatCache.getUserIdToChatIn()
                .get(messageVO.getUserId()).getOneChatVOListMap().get(oneToOneChatOld.getUserId2()));
        chatVOS.addAll(chatCache.getUserIdToChatIn()
                .get(oneToOneChatOld.getUserId2()).getOneChatVOListMap().get(messageVO.getUserId()));
        Collections.sort(chatVOS, (c1, c2) -> (int) (c1.getCreateTime() - c2.getCreateTime()));
        chatRouteUtil.sendChatResponse(PtoNum.ONETOONECHATOLD, messageVO.getUserId(), chatVOS);
    }
}
